package com.hgnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgnu.common.R;
import com.hgnu.dto.DishDto;
import com.hgnu.pojo.Category;
import com.hgnu.pojo.Dish;
import com.hgnu.pojo.DishFlavor;
import com.hgnu.service.CategoryService;
import com.hgnu.service.DishFlavorService;
import com.hgnu.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    //分类查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name) {
        //构造分页构造器
        Page<Dish> pageinfo = new Page(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //添加条件过滤器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行查询
        dishService.page(pageinfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageinfo,dishDtoPage,"records");
        List<Dish> records = pageinfo.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //分类ID
            Long categoryId = item.getCategoryId();
            //根据ID查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }
    //新增菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    //根据ID查询菜品信息和对应的口味信息
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }
    /*//停售菜品
    @PostMapping("/status/{st}")
    public R<String> updateStatus(@PathVariable int st, String ids){

        String[] dishid = ids.split(",");
        List<Long> idlist = Arrays.stream(dishid).map(s ->
            Long.parseLong(s)
        ).collect(Collectors.toList());
        List<Dish> dishList = idlist.stream().map(s -> {
            Dish dish = new Dish();
            dish.setId(s);
            dish.setStatus(st);
            return dish;
        }).collect(Collectors.toList());
        dishService.updateBatchById(dishList);
        return R.success("员工状态修改成功");
    }*/
   //停售（起售）菜品
    @PostMapping("/status/{st}")
    public R<String> updateStatus(@PathVariable int st,Long[] ids){
        List<Long> idlist = Arrays.asList(ids);
        //构建条件构造器
        LambdaUpdateWrapper<Dish> updateChainWrapper = new LambdaUpdateWrapper<>();
        //添加过滤条件
        updateChainWrapper.set(Dish::getStatus,st).in(Dish::getId,idlist);
        dishService.update(updateChainWrapper);
        return R.success("菜品状态修改成功");
    }
    //删除菜品
    @DeleteMapping
    public R<String> delete(String ids){
        String[] dishid = ids.split(",");
        List<Long> dishlit = Arrays
                .stream(dishid)
                .map(s -> Long.parseLong(s))
                .collect(Collectors.toList());
        dishService.removeByIds(dishlit);
        return R.success("删除成功");
    }
    //查询对应分类菜品
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //构建条件构建器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        queryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishDto> dishDtoList = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //分类ID
            Long categoryId = item.getCategoryId();
            //根据ID查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }
}
